package sysConfig

import (
	"context"
	"devops-super/api/configs/v1"
	"devops-super/internal/model/entity"
	"devops-super/internal/service"
	"github.com/gogf/gf/v2/os/gtime"
)

// GetPageLst 分页获取系统配置列表
func (*ControllerV1) GetPageLst(ctx context.Context, req *v1.SysConfigGetPageLstReq) (res *v1.SysConfigGetPageLstRes, err error) {
	res = &v1.SysConfigGetPageLstRes{}
	res.PageLstRes, err = service.SysConfig().GetPageLst(ctx, req)
	return
}

// Add 新增系统配置
func (*ControllerV1) Add(ctx context.Context, req *v1.SysConfigAddReq) (res *v1.SysConfigAddRes, err error) {
	config := &entity.SysConfig{
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		CreatedAt:   gtime.Now(),
	}
	err = service.SysConfig().Add(ctx, config)
	return
}

// Upt 更新系统配置
func (*ControllerV1) Upt(ctx context.Context, req *v1.SysConfigUptReq) (res *v1.SysConfigUptRes, err error) {
	config := &entity.SysConfig{
		Id:          int64(req.Id),
		GroupName:   req.GroupName,
		ConfigKey:   req.ConfigKey,
		ConfigValue: req.ConfigValue,
		Remark:      req.Remark,
		UpdatedAt:   gtime.Now(),
	}
	err = service.SysConfig().Upt(ctx, config)
	return
}

// Del 删除系统配置
func (*ControllerV1) Del(ctx context.Context, req *v1.SysConfigDelReq) (res *v1.SysConfigDelRes, err error) {
	err = service.SysConfig().Del(ctx, req.Id)
	return
}

// GetOne 获取单条系统配置
func (*ControllerV1) GetOne(ctx context.Context, req *v1.SysConfigGetOneReq) (res *v1.SysConfigGetOneRes, err error) {
	info, err := service.SysConfig().GetOne(ctx, req.Id)
	if err != nil {
		return
	}
	res = &v1.SysConfigGetOneRes{Info: info}
	return
}

// GetOptions 获取系统配置选项
func (*ControllerV1) GetOptions(ctx context.Context, req *v1.SysConfigGetOptionsReq) (res *v1.SysConfigGetOptionsRes, err error) {
	res, err = service.SysConfig().GetOptions(ctx, req)
	return
}

// GetGroupConfigs 获取系统配置组
func (*ControllerV1) GetGroupConfigs(ctx context.Context, req *v1.GetSysGroupConfigsReq) (res *v1.GetSysGroupConfigsRes, err error) {
	res, err = service.SysConfig().GetGroupConfigs(ctx, req)
	return
}

// SetGroupConfigs 保存系统配置组
func (*ControllerV1) SetGroupConfigs(ctx context.Context, req *v1.SetSysGroupConfigsReq) (res *v1.SetSysGroupConfigsRes, err error) {
	res = &v1.SetSysGroupConfigsRes{}
	err = service.SysConfig().SetGroupConfigs(ctx, req)
	return
}
